﻿using DmxLib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DmxTestTool.Model
{
    public delegate void DMXViewUIModelEvent(object sender,string action);
    public class DMXViewUIModel : System.ComponentModel.INotifyPropertyChanged,IDisposable
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public event DMXViewUIModelEvent DMXViewUIModelEvent;
        #region UI
        private string dmxCtrContent = "启动发送";
        public string DMXCtrContent
        {
            set
            {
                dmxCtrContent = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("DMXCtrContent"));
            }
            get => dmxCtrContent;
        }
        public UserCommand BtnCommand { private set; get; }
        public UserCommand CloseBtn { private set; get; }

        #endregion
        public IDmxSend DmxSend { set; get; }
        private List<DmxViewItemModel> itemSource;
        public List<DmxViewItemModel> ItemSource
        {
            set
            {
                this.itemSource = value;
                PropertyChanged?.Invoke(this,new PropertyChangedEventArgs("ItemSource"));
            }
            get => itemSource;
        }
        public string DMXName { set; get; } = "DMX测试";
     
        public DMXTestInfo DMXTestInfo { set; get; }
        public long ID { set; get; }
        private System.Timers.Timer Timer;
     
        public DMXViewUIModel(DMXTestInfo info)
        {
            try
            {
                DMXName = info.TitleName;
                ID = DateTime.Now.Ticks;
                this.DMXTestInfo = info;
                List<DmxViewItemModel> list = new List<DmxViewItemModel>();
                for (int i = DMXTestInfo.StartIndex; i <= DMXTestInfo.EndIndex; i++)
                {
                    list.Add(new DmxViewItemModel() { Index = i });
                }
                ItemSource = list;
                BtnCommand = new UserCommand();
                BtnCommand.CommandExecute += BtnCommand_CommandExecute;
                CloseBtn = new UserCommand();
                CloseBtn.CommandExecute += (p) =>
                {
                    DMXViewUIModelEvent?.BeginInvoke(this, "CloseTab", null, null);
                };

                if (info.sendType == 0)
                {
                    DmxSend = new ArtNetSend(System.Net.IPAddress.Parse(info.IP)
                        , VCLib.Converts.IPConverts.ConverToIPEndPoint(info.LocalIPEndPoint),
                        info.Universe, info.Frame
                        );
                }
                else if (info.sendType == 1)
                {
                    DmxSend = new Rs485DmxSend(info.ComName);
                }
                else if (info.sendType == 2)
                {
                    DmxSend = new Comm.OscSend(info.OSCIPEndPoint);
                }
                Timer = new System.Timers.Timer(1000/info.Frame);
                Timer.Elapsed += Timer_Elapsed; 
                Timer.Start();
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show("创建失败：" + ex.Message);

            }


        }

        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (DmxSend != null)
            {
                byte[] data = new byte[ItemSource.Count];
                for (int i = 0; i < ItemSource.Count; i++)
                {
                    data[i] = ItemSource[i].Value;
                }
                DmxSend.UpdateData(DMXTestInfo.StartIndex-1,data);
            }
        }

        private void BtnCommand_CommandExecute(object parameter)
        {
            string str = parameter.ToString();
            switch (str)
            {
                case "AllON":
                    foreach (var item in ItemSource)
                    {
                        item.Value = 255;
                    }
                    break;
                case "AllOFF":
                    foreach (var item in ItemSource)
                    {
                        item.Value = 0;
                    }
                    break;
                case "DMXCtr":
                    if (DmxSend != null)
                    {
                        try
                        {
                            if (DmxSend.IsStart)
                            {
                                DmxSend.Stop();
                                DMXCtrContent = "启动发送";
                            }
                            else
                            {
                                DmxSend.Start();
                                DMXCtrContent = "停止发送";
                            }
                        }
                        catch (Exception ex)
                        {
                            System.Windows.MessageBox.Show("操作失败："+ex.Message);
                        }

                    }
                    else
                    {
                        System.Windows.MessageBox.Show("操作失败：DmxSend 未实例化");
                    }
                  
                    break;
              
                default:
                    break;
            }
        }

        public void Dispose()
        {
            Timer.Stop();
            if (DmxSend != null)
            {
                DmxSend.Dispose();
            }
        }
    }
}
